package myATMCaseV1;

import jdbcUtils.JdbcUtilsV5;
import java.math.BigDecimal;

public class AccountBLL {
    AccountData accountData = new AccountData();

    //登陆账户（通过username和password）
    public Account getAccount(Integer uid, String password){
        Account account = null;
        try {
            JdbcUtilsV5.threadConnectionStart();//开启事务
            account = accountData.accountLogin(uid, password);
            JdbcUtilsV5.threadConnectionCommit();//提交事物
            return account;
        } catch (Exception e) {
            JdbcUtilsV5.threadConnectionRollback();//回滚事物
        }
        return account;
    }

    //查询业务（通过uid）
    public Account queryAccount(Integer uid){
        Account account = null;
        try {
            JdbcUtilsV5.threadConnectionStart();//开启事务
            account = accountData.accountQuery(uid);
            JdbcUtilsV5.threadConnectionCommit();//提交事物
            return account;
        } catch (Exception e) {
            JdbcUtilsV5.threadConnectionRollback();//回滚事物
        }
        return account;
    }

    //转账业务
    public boolean transfer(Account from, Account to, BigDecimal money){
        try {
            JdbcUtilsV5.threadConnectionStart();//开启事务
            from.setBalance(from.getBalance().subtract(money));
            accountData.accountUpdate(from);
            to.setBalance(to.getBalance().add(money));
            accountData.accountUpdate(to);
            JdbcUtilsV5.threadConnectionCommit();//提交事物
            return true;//转账成功
        } catch (Exception e) {
            JdbcUtilsV5.threadConnectionRollback();//回滚事物
        }
        return false;//转账失败
    }

    //取款业务
    public boolean withdrawal(Account account, BigDecimal money){
        try {
            JdbcUtilsV5.threadConnectionStart();//开启事务
            account.setBalance(account.getBalance().subtract(money));
            accountData.accountUpdate(account);
            JdbcUtilsV5.threadConnectionCommit();//提交事物
            return true;//取款成功
        } catch (Exception e) {
            JdbcUtilsV5.threadConnectionRollback();//回滚事物
        }
        return false;//取款失败
    }

    //存款业务
    public boolean deposit(Account account, BigDecimal money){
        try {
            JdbcUtilsV5.threadConnectionStart();//开启事务
            account.setBalance(account.getBalance().add(money));
            accountData.accountUpdate(account);
            JdbcUtilsV5.threadConnectionCommit();//提交事物
            return true;//存款成功
        } catch (Exception e) {
            JdbcUtilsV5.threadConnectionRollback();//回滚事物
        }
        return false;//存款失败
    }

    //修改信息业务
    public boolean modifyInfo(Account newAccount){
        try {
            JdbcUtilsV5.threadConnectionStart();//开启事务
            accountData.accountUpdate(newAccount);
            JdbcUtilsV5.threadConnectionCommit();//提交事物
            return true;//修改成功
        } catch (Exception e) {
            JdbcUtilsV5.threadConnectionRollback();//回滚事物
        }
        return false;//修改失败
    }

}
